﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ControlesAWIL
{
    //-- esto es para que el campo se pueda asociar a un bindingSource en tiempo de diseño.
    [System.ComponentModel.DefaultBindingProperty("Text")]
    public partial class TextBoxCOTEC : TextBox
    {
        private bool mostrarZoom = false;
        private FormatoTexto formatoTexto = FormatoTexto.Texto;
        private int numeroDecimales = 0;
        
        public bool COTEC_MostrarZoom
        {
            get { return mostrarZoom; }
            set
            {
                if (!this.Multiline && this.mostrarZoom)
                {
                    MessageBox.Show("Esta Propiedad Solo aplica Campos Multilinea");
                    return;
                }
                mostrarZoom = value;
            }
        }

        public int AWIL_NumeroDecimales
        {
            get { return numeroDecimales; }
            set
            {
                if (formatoTexto == FormatoTexto.Texto && this.numeroDecimales!=0) {
                    MessageBox.Show("Esta Propiedad Solo aplica Para Numeros");
                    this.numeroDecimales = 0;
                    return;
                }
                if (value > 0 && value < 10)
                {
                    this.numeroDecimales = value;
                }
                else
                {
                    this.numeroDecimales = 0;
                }
            }
        }

        public FormatoTexto AWIL_FormatoTexto
        {
            get { return formatoTexto; }
            set
            {
                if (value == FormatoTexto.Texto)
                {
                    this.numeroDecimales = 0;
                    this.TextAlign = HorizontalAlignment.Left;
                }
                else 
                {
                    this.TextAlign = HorizontalAlignment.Right;
                }
                formatoTexto = value;
            }
        }

        public TextBoxCOTEC()
        {
            InitializeComponent();
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
        }

        private void MSTextBox_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (this.Multiline && this.mostrarZoom)
            {
                VentanasEmergentes.getVentanaTextBox().ok = false;
                VentanasEmergentes.getVentanaTextBox().Texto = this.Text;
                VentanasEmergentes.getVentanaTextBox().TextoMaxLength = this.MaxLength;
                VentanasEmergentes.getVentanaTextBox().TextoEnabled = this.Enabled;
                VentanasEmergentes.getVentanaTextBox().TextoReadOnly = this.ReadOnly;

                VentanasEmergentes.getVentanaTextBox().ShowDialog();
                if (VentanasEmergentes.getVentanaTextBox().ok)
                {
                    this.Text = VentanasEmergentes.getVentanaTextBox().Texto;
                }
                this.Focus();
            }
        }

        private void MSTextBox_Leave(object sender, EventArgs e)
        {
            this.ForeColor = Color.Black;

            if (this.formatoTexto == global::ControlesAWIL.FormatoTexto.Numero)
            {
                decimal temp = 0;
                if (!this.Text.Trim().Equals(string.Empty) && !decimal.TryParse(this.Text, out temp))
                {
                    MessageBox.Show("Por Favor Digite Un Numero Valido.");
                    this.ForeColor = Color.Red;
                    this.Focus();
                }
                else if (!this.Text.Trim().Equals(string.Empty))
                {
                    this.Text = temp.ToString("#,0." + new string('0', this.numeroDecimales)); //"#,0.00"
                    //this.Text = Math.Round(temp, this.NumeroDecimales).ToString();
                }
            }
        }


        private void AWTextBox_Enter(object sender, EventArgs e)
        {
            if (this.formatoTexto == global::ControlesAWIL.FormatoTexto.Numero)
            {
                decimal temp = 0;
                if (!this.Text.Trim().Equals(string.Empty) && !decimal.TryParse(this.Text, out temp))
                {
                    this.ForeColor = Color.Red;
                }
                else if (!this.Text.Trim().Equals(string.Empty))
                {
                    this.Text = temp.ToString("#,0." + new string('0', this.numeroDecimales)); //"#,0.00"
                    this.Text = Math.Round(temp, this.numeroDecimales).ToString();
                }
            }

        }

        private void MSTextBox_MultilineChanged(object sender, EventArgs e)
        {
            if (this.Multiline)
            {
                this.mostrarZoom = true;
            }
            else 
            {
                this.mostrarZoom = false;
            }
        }

        private void MSTextBox_EnabledChanged(object sender, EventArgs e)
        {
            if (!this.Enabled)
            {
                this.ReadOnly = true;
                this.BackColor = Color.WhiteSmoke;
                this.Font = new System.Drawing.Font("Arial", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.Enabled = false;
                //  this.Height = 23;
            }
            else
            {
                this.ReadOnly = false;
                this.BackColor = Color.White;
                this.Font = new System.Drawing.Font("Arial", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.Enabled = true;
                //this.Height = 20;
            }
        }
    }
}

